package com.kaikeba.wx.controller;

import com.kaikeba.bean.CourierExpress;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.UserExpress;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierExpressService;
import com.kaikeba.service.UserExpressService;
import com.kaikeba.util.JSONUtil;
import com.kaikeba.util.RandomUtil;
import com.kaikeba.util.SMSUtil;
import com.kaikeba.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {

    @ResponseBody("/wx/loginSms.do")   //获取登陆地址
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String code = RandomUtil.getCode() + "";
        boolean flag = SMSUtil.send(userPhone, code);
        Message msg = new Message();
        if(flag){
            //短信发送成功
            msg.setStatus(0);
            msg.setResult("验证码已发送，请查收！");
        }else{
            //短信发送失败
            msg.setStatus(-1);
            msg.setResult("验证码发送失败,请检查手机或稍后再试!");
        }
        UserUtil.setLoginSms(request.getSession(),userPhone,code);
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/wx/login.do")   //微信登陆
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String userCode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        Message msg = new Message();
        UserExpress user = new UserExpress();
        if(sysCode == null){
            //手机号未获取短信
            msg.setStatus(-1);
            msg.setResult("手机号码未获取短信");
        }else if(sysCode.equals(userCode)){
            //手机号码和短信一致，登陆成功
            //判断手机号是快递员的还是用户的
            CourierExpress byPhone = CourierExpressService.findByPhone(userPhone);
            if(byPhone != null){
                //查到信息了  证明是快递员登录
                msg.setStatus(1);
                user.setUser(false);
                msg.setResult("快递员登录成功");
            }else{
                //证明是用户登录
                msg.setStatus(0);
                user.setUser(true);
                msg.setResult("用户登录成功");
            }
            user.setUserPhone(userPhone);
            UserUtil.setStatus(request.getSession(),msg.getStatus());
            UserUtil.setWxUser(request.getSession(),user);
        }else{
            //这里是验证码不一致，登陆失败
            msg.setStatus(-2);
            msg.setResult("验证码不一致,请检查");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/logout.do")   //微信退出登录
    public String logout(HttpServletRequest request, HttpServletResponse response){

        //1.销毁session
        request.getSession().invalidate();
        //2.给客户端回复消息
        Message msg = new Message(0);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/userInfo.do")   //获取用户信息
    public String userinfo(HttpServletRequest request, HttpServletResponse response){
        UserExpress user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        Message msg = new Message();
        if(isUser) {
            msg.setStatus(0);
        } else{
            msg.setStatus(1);
        }
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/wx/insertUser.do")
    public String insertUser(HttpServletRequest request, HttpServletResponse response){
        //获取前台信息
        String userPhone = request.getParameter("userPhone");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String idCard = request.getParameter("idCard");
        String msg1 = request.getParameter("msg");
        //获取后端发送的验证码  然后判断两个验证码是否正确
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        //判断手机号码与身份证号是否重复
        UserExpress byPhone = UserExpressService.findByPhone(userPhone);
        UserExpress byIdCard = UserExpressService.findByIdCard(idCard);
        Message msg = new Message();
        //如果查询出这两个都为null   就表明没用重复
        if(sysCode.equals(msg1)){
            //验证码输入正确
            if(byIdCard == null && byPhone == null) {
                UserExpress e = new UserExpress(username, userPhone, idCard, password);
                boolean insert = UserExpressService.insert(e);
                if (insert) {
                    msg.setResult("注册成功！");
                    msg.setStatus(0);
                }else {
                    msg.setResult("抱歉,注册失败~");
                    msg.setStatus(-1);
                }
            }else {
                msg.setResult("抱歉,注册失败~,手机号或身份证号已被注册");
                msg.setStatus(-1);
            }
        }else{
            msg.setResult("验证码错误,请重新输入");
            msg.setStatus(-1);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/userName.do")
    public String userName(HttpServletRequest request,HttpServletResponse response){
        //获取存的信息
        UserExpress user = UserUtil.getWxUser(request.getSession());
        //判断用户是否存在
        UserExpress byPhone = UserExpressService.findByPhone(user.getUserPhone());
        //判断快递员是否存在
        CourierExpress byPhone1 = CourierExpressService.findByPhone(user.getUserPhone());
        Message msg = new Message();

        if(byPhone1 != null){
            msg.setStatus(1);
            msg.setResult(byPhone1.getUsername());
        }else if(byPhone != null){
            msg.setStatus(0);
            msg.setResult(byPhone.getUsername());
        }else{
            msg.setStatus(-1);
            msg.setResult("姓名");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/flagInfo.do")
    public String flagInfo(HttpServletRequest request,HttpServletResponse response){
        //判断登陆过后  是新用户还是注册过的用户
        //获取登陆的电话号码
        UserExpress wxUser = UserUtil.getWxUser(request.getSession());
        String userPhone = wxUser.getUserPhone();
        //通过service方法调取查询
        UserExpress userExpress = UserExpressService.findByPhone(userPhone);
        CourierExpress courierExpress = CourierExpressService.findByPhone(userPhone);
        Message msg = new Message();
        if(userExpress != null || courierExpress != null){
            //证明被注册过
            msg.setStatus(0);
        }else {
            //没被注册过
            msg.setStatus(-1);
        }
        return JSONUtil.toJSON(msg);
    }

}
